* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization
*    "The Visibility of Bracelets by Treatment Group"
* **************************************************************************** *
/*
** Purpose:  Generates the table:
             "The Visibility of Bracelets by Treatment Group"

*/
* **************************************************************************** *
* **************************************************************************** *


* **************************************************************************** *
* The Visibility of Bracelets by Treatment Group - with controls -
* **************************************************************************** *



	* Load First-Order beliefs:
	use  "${Replicate_SocialSignals_dtaInter}/FirstOrderBeliefs_Data.dta", clear

	* Append Second-Order Beliefs:
	append using "${Replicate_SocialSignals_dtaInter}/SecondOrderBeliefs_Data.dta", gen(SecondOrder_Beliefs)


	gen outcome1 = otherchildbracelet        if age_u1>= 107 & age_u1<=365
	gen outcome2 = otherchildcolor           if age_u1>= 107 & age_u1<=365
	gen outcome3 = secondorder_bracelet      if age_pikin>=107& age_pikin<=365
	gen outcome4 = knows_bracelet_end        if age_u1>= 107 & age_u1<=365
	gen outcome5 = knows_color_end2          if age_u1>= 107 & age_u1<=365


	* --------------------------------------------------------------------------
	* Demean First-Order Controls:

	* drop existing variables
	drop  *_first*  *_second*


	tab  arm_anc, gen(anc)

	foreach anc_treat of numlist 2,3,4 {
		foreach outcome of numlist 1,2,4,5 {
			summ  anc`anc_treat'  if outcome`outcome' !=. & age_u1>= 107 & age_u1<=365 & intervention_arm==2, detail
			gen   anc`anc_treat'_first_dm`outcome' = anc`anc_treat' - `r(mean)'       if outcome`outcome' !=. & age_u1>= 107 & age_u1<=365
		}
	}

	local FO_Controls = " mother_age_w01 edu2 edu3 farm relate birth2 birth3 birth4 birth5 age_u1 "

	summ `FO_Controls'   if age_u1>= 107 & age_u1<=365


	foreach control of local FO_Controls {
		foreach outcome of numlist 1,2,4,5 {
			summ  `control'                                                     if outcome`outcome'!=. & age_u1>= 107 & age_u1<=365 & intervention_arm==2, detail
			gen   `control'_first_dm`outcome' = `control' - `r(mean)'                 if outcome`outcome' !=. & age_u1>= 107 & age_u1<=365
		}
	}


	* --------------------------------------------------------------------------
	* Demean Second-Order Controls:

	foreach anc_treat of numlist 2,3,4 {
		foreach outcome of numlist 3 {
			summ  anc`anc_treat'                                                if outcome`outcome' !=. & age_pikin>=107& age_pikin<=365 & intervention_arm==2, detail
			gen   anc`anc_treat'_second_dm`outcome' = anc`anc_treat' - `r(mean)'       if outcome`outcome' !=. & age_pikin>=107& age_pikin<=365
		}
	}


	local SO_Controls = " mother_age_w01 edu2 edu3 farm relate birth2 birth3 birth4 birth5 age_pikin "

	foreach control of local SO_Controls {
		foreach outcome of numlist 3 {
			summ  `control'                                                      if outcome`outcome'!=.  & age_pikin>=107& age_pikin<=365 & intervention_arm==2, detail
			gen   `control'_second_dm`outcome' = `control' - `r(mean)'                if outcome`outcome' !=. & age_pikin>=107& age_pikin<=365
		}
	}



	* Store Controls in locals:

	local FO_Mother_Controls_107_1 = " mother_age_w01_first_dm1 edu2_first_dm1 edu3_first_dm1 farm_first_dm1 relate_first_dm1  "
	local FO_Child_Controls_107_1  = " birth2_first_dm1 birth3_first_dm1 birth4_first_dm1 birth5_first_dm1 age_u1_first_dm1 "

	local FO_Mother_Controls_107_2 = " mother_age_w01_first_dm2 edu2_first_dm2 edu3_first_dm2 farm_first_dm2 relate_first_dm2  "
	local FO_Child_Controls_107_2  = " birth2_first_dm2 birth3_first_dm2 birth4_first_dm2 birth5_107_dm2 age_u1_dm2 "

	local SO_Mother_Controls_107_3 = " mother_age_w01_second_dm3 edu2_second_dm3 edu3_second_dm3 farm_second_dm3 relate_second_dm3  "
	local SO_Controls_107_3        = " birth2_second_dm3 birth3_second_dm3 birth4_second_dm3 birth5_second_dm3 age_pikin_second_dm3  "

	local FO_Mother_Controls_107_4 = " mother_age_w01_first_dm4 edu2_first_dm4 edu3_first_dm4 farm_first_dm4 relate_first_dm4  "
	local FO_Child_Controls_107_4  = " birth2_first_dm4 birth3_first_dm4 birth4_first_dm4 birth5_first_dm4 age_u1_first_dm4 "

	local FO_Mother_Controls_107_5 = " mother_age_w01_first_dm5 edu2_first_dm5 edu3_first_dm5 farm_first_dm5 relate_first_dm5  "
	local FO_Child_Controls_107_5  = " birth2_first_dm5 birth3_first_dm5 birth4_first_dm5 birth5_first_dm5 age_u1_first_dm5 "


	summ  otherchildbracelet     treat3 treat4     anc2_first_dm1  anc3_first_dm1  anc4_first_dm1   `FO_Mother_Controls_107_1' `FO_Controls_107_1'    if age_u1>= 107 & age_u1<=365

	summ  otherchildcolor        treat3 treat4     anc2_first_dm2  anc3_first_dm2  anc4_first_dm2   `FO_Mother_Controls_107_2' `FO_Controls_107_2'    if age_u1>= 107 & age_u1<=365

	summ  secondorder_bracelet   treat3 treat4     anc2_second_dm3 anc3_second_dm3 anc4_second_dm3  `SO_Mother_Controls_107_3' `SO_Controls_107_3'    if age_pikin>=107& age_pikin<=365

	summ  knows_bracelet_end     treat3 treat4     anc2_first_dm4  anc3_first_dm4  anc4_first_dm4   `FO_Mother_Controls_107_4' `FO_Controls_107_4'    if age_u1>= 107 & age_u1<=365

	summ  knows_color_end2       treat3 treat4     anc2_first_dm5  anc3_first_dm5  anc4_first_dm5   `FO_Mother_Controls_107_5' `FO_Controls_107_5'    if age_u1>= 107 & age_u1<=365



	** First order beliefs
	eststo clear
	areg     otherchildbracelet   treat3 treat4     anc2_first_dm1  anc3_first_dm1  anc4_first_dm1   `FO_Mother_Controls_107_1' `FO_Controls_107_1'    if age_u1>= 107 & age_u1<=365, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo model_otherbracelet

	areg     otherchildcolor     treat3 treat4       anc2_first_dm2  anc3_first_dm2  anc4_first_dm2     `FO_Mother_Controls_107_2' `FO_Controls_107_2'   if age_u1>= 107 & age_u1<=365, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo model_othercolor

	* Second Order Bracelet Beliefs:
	areg     secondorder_bracelet   treat3 treat4     anc2_second_dm3 anc3_second_dm3 anc4_second_dm3    `SO_Mother_Controls_107_3' `SO_Controls_107_3'   if age_pikin>=107& age_pikin<=365, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo model_secondbracelet

	areg     knows_bracelet_end     treat3 treat4    anc2_first_dm4  anc3_first_dm4  anc4_first_dm4   `FO_Mother_Controls_107_4' `FO_Controls_107_4'   if age_u1>= 107 & age_u1<=365, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo m_knows_bracelet

	areg     knows_color_end2      treat3 treat4     anc2_first_dm5  anc3_first_dm5  anc4_first_dm5     `FO_Mother_Controls_107_5' `FO_Controls_107_5'   if age_u1>= 107 & age_u1<=365, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo m_knows_color

	esttab ///
	model_otherbracelet  model_othercolor model_secondbracelet           ///
	m_knows_bracelet  m_knows_color                                      ///
	using "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex",  ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule \\"                      ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Know if other child}}  & \multicolumn{1}{c}{\textbf{Know other child's}} & \multicolumn{1}{c}{\textbf{Others know if own child has}} & \multicolumn{1}{c}{\textbf{Correct knowledge:}}         & \multicolumn{1}{c}{\textbf{Correct knowledge:}}  \\"    ///
	"                                                 & \multicolumn{1}{c}{\textbf{has a bracelet}}       & \multicolumn{1}{c}{\textbf{bracelet color}}     & \multicolumn{1}{c}{\textbf{a yellow or green bracelet}}   & \multicolumn{1}{c}{\textbf{other child has a bracelet}} & \multicolumn{1}{c}{\textbf{other child's bracelet color}}  \\")  ///
	scalars("C_mean        Uninformative Bracelet Group mean"            ///
	"Obs           Observations"                                 ///
	"treat3_treat4 p(S4 = S5)"                                   ///
	"controls     Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))               ///
	keep(_cons treat3 treat4)                                            ///
	varlabels(_cons "Uninformative Bracelet Group mean")                 ///
	sfmt(3) ${StarsOpt}  nomtitles tex                                   ///
	addnotes("") label b(3) se(3) nobaselevels noconstant noobs         ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}")     ///
	replace

	filefilter "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex"      ///
	"${Replicate_SocialSignals_Tables}/Table_VisibilityOfBracelet_Controls.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex"


	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls

	esttab ///
	model_otherbracelet  model_othercolor model_secondbracelet           ///
	m_knows_bracelet  m_knows_color                                      ///
	using "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex",  ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule \\"                      ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Know if other child}}  & \multicolumn{1}{c}{\textbf{Know other child's}} & \multicolumn{1}{c}{\textbf{Others know if own child has}} & \multicolumn{1}{c}{\textbf{Correct knowledge:}}         & \multicolumn{1}{c}{\textbf{Correct knowledge:}}  \\"    ///
	"                                                 & \multicolumn{1}{c}{\textbf{has a bracelet}}       & \multicolumn{1}{c}{\textbf{bracelet color}}     & \multicolumn{1}{c}{\textbf{a yellow or green bracelet}}   & \multicolumn{1}{c}{\textbf{other child has a bracelet}} & \multicolumn{1}{c}{\textbf{other child's bracelet color}}  \\")  ///
	scalars("C_mean        Uninformative Bracelet Group mean"            ///
	"Obs           Observations"                                 ///
	"treat3_treat4 p(S4 = S5)"                                   ///
	"controls     Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))               ///
	keep(_cons treat3 treat4)                                            ///
	varlabels(_cons "Uninformative Bracelet Group mean")                 ///
	sfmt(3) ${StarsOpt}  nomtitles tex                                   ///
	addnotes("") label b(3) p(3) nobaselevels noconstant noobs         ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}")     ///
	replace

	filefilter "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex"      ///
	"${Replicate_SocialSignals_Tables}/pvalues/Table_VisibilityOfBracelet_Controls_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_BraceletKnowledge_raw.tex"





********************************************************************************
** End of the Dofile !!!
********************************************************************************
